﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x0200024A RID: 586
	public class ModelConfigView : BaseView
	{
		// Token: 0x06000CAA RID: 3242 RVA: 0x000668D4 File Offset: 0x00064AD4
		protected override void Awake()
		{
			base.Awake();
			this.pairBoneButton.onClick.AddListener(new UnityAction(this.OnPairBones));
			this.showHiddenButton.onClick.AddListener(new UnityAction(this.OnShowHidden));
			this.visibilityButton.onClick.AddListener(new UnityAction(this.OnVisibility));
			this.bodyViewButton.onClick.AddListener(delegate()
			{
				this.OnViewSelect(ViewName.Body);
			});
			this.hairViewButton.onClick.AddListener(delegate()
			{
				this.OnViewSelect(ViewName.Hair);
			});
			this.extraViewButton.onClick.AddListener(delegate()
			{
				this.OnViewSelect(ViewName.Extra);
			});
			this.handleSizeSlider.onValueChanged.AddListener(new UnityAction<float>(this.OnHandleSize));
			this.OnViewSelect(ViewName.Body);
			this.hideHeadToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideHeadBones));
			this.hideTorsoToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideTorsoBones));
			this.hideArmsToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideArmBones));
			this.hideLegsToggle.onValueChanged.AddListener(new UnityAction<bool>(this.HideLegBones));
			this.saveButton.onClick.AddListener(new UnityAction(this.OnSave));
			this.discardButton.onClick.AddListener(new UnityAction(this.OnDiscard));
		}

		// Token: 0x06000CAB RID: 3243 RVA: 0x00066A5C File Offset: 0x00064C5C
		protected override void OnEnable()
		{
			base.OnEnable();
			if (this.previousTarget != this.handleManager.TargetEditor)
			{
				this.previousTarget = this.handleManager.TargetEditor;
				this.SetConfigDirty(false);
			}
			this.UpdateKeys();
			this.ShowHandles();
			this.handleManager.SetHandleSize(this.handleSizeSlider.value);
			HandleManager handleManager = this.handleManager;
			handleManager.onHandleSelection = (HandleManager.OnHandleSelect)Delegate.Combine(handleManager.onHandleSelection, new HandleManager.OnHandleSelect(this.OnHandleSelect));
			HandleManager handleManager2 = this.handleManager;
			handleManager2.onHandleDeselection = (HandleManager.OnHandleSelect)Delegate.Combine(handleManager2.onHandleDeselection, new HandleManager.OnHandleSelect(this.OnHandleDeselect));
		}

		// Token: 0x06000CAC RID: 3244 RVA: 0x00066B14 File Offset: 0x00064D14
		protected override void OnDisable()
		{
			base.OnDisable();
			this.handleManager.DisableHandlesMode();
			HandleManager handleManager = this.handleManager;
			handleManager.onHandleSelection = (HandleManager.OnHandleSelect)Delegate.Remove(handleManager.onHandleSelection, new HandleManager.OnHandleSelect(this.OnHandleSelect));
			HandleManager handleManager2 = this.handleManager;
			handleManager2.onHandleDeselection = (HandleManager.OnHandleSelect)Delegate.Remove(handleManager2.onHandleDeselection, new HandleManager.OnHandleSelect(this.OnHandleDeselect));
		}

		// Token: 0x06000CAD RID: 3245 RVA: 0x00066B80 File Offset: 0x00064D80
		private void Update()
		{
			this.ModelConfigGui();
		}

		// Token: 0x06000CAE RID: 3246 RVA: 0x00066B88 File Offset: 0x00064D88
		private void ModelConfigGui()
		{
			int count = this.handleManager.TargetHandles.Count;
			if (count == 2)
			{
				this.pairBoneButton.interactable = true;
			}
			else
			{
				this.pairBoneButton.interactable = false;
			}
		}

		// Token: 0x06000CAF RID: 3247 RVA: 0x00066BCA File Offset: 0x00064DCA
		private void UpdateKeys()
		{
			if (this.configIsDirty)
			{
				return;
			}
			this.bodyView.UpdateKeys();
			this.hairView.UpdateKeys();
			this.extraView.UpdateKeys();
		}

		// Token: 0x06000CB0 RID: 3248 RVA: 0x00066BF9 File Offset: 0x00064DF9
		private List<string> GetKeys(ViewName view)
		{
			switch (view)
			{
			case ViewName.Body:
				return this.bodyView.Keys;
			case ViewName.Hair:
				return this.hairView.Keys;
			case ViewName.Extra:
				return this.extraView.Keys;
			default:
				return null;
			}
		}

		// Token: 0x06000CB1 RID: 3249 RVA: 0x00066C38 File Offset: 0x00064E38
		private void SetConfigDirty(bool value)
		{
			this.configIsDirty = value;
			if (this.configIsDirty)
			{
				this.saveButton.interactable = true;
				this.discardButton.interactable = true;
			}
			else
			{
				this.saveButton.interactable = false;
				this.discardButton.interactable = false;
			}
		}

		// Token: 0x06000CB2 RID: 3250 RVA: 0x00066C8C File Offset: 0x00064E8C
		private void ShowHandles()
		{
			if (!this.handleManager.TargetEditor)
			{
				return;
			}
			this.handleManager.DisableHandlesMode();
			if (this.showingHidden)
			{
				Dictionary<string, EditBone>.KeyCollection keys = this.handleManager.TargetEditor.SkeletonEdit.BoneMap.Keys;
				List<string> keys2 = new List<string>(keys);
				List<string> keys3 = this.GetKeys(this.currentView);
				this.handleManager.EnableHandlesMode(null, keys2);
				this.handleManager.SetHandleStyle(SkeletonEditHandle.HandleStyle.Active, keys3, true);
			}
			else
			{
				List<string> keys4 = this.GetKeys(this.currentView);
				this.handleManager.EnableHandlesMode(null, keys4);
				this.handleManager.SetHandleStyle(SkeletonEditHandle.HandleStyle.Active, keys4, false);
			}
			this.handleManager.DisableHandles(this.hiddenKeys);
		}

		// Token: 0x06000CB3 RID: 3251 RVA: 0x00066D50 File Offset: 0x00064F50
		private void OnViewSelect(ViewName view)
		{
			this.currentView = view;
			this.bodyViewButton.interactable = true;
			this.hairViewButton.interactable = true;
			this.extraViewButton.interactable = true;
			switch (view)
			{
			case ViewName.Body:
				this.bodyViewButton.interactable = false;
				break;
			case ViewName.Hair:
				this.hairViewButton.interactable = false;
				break;
			case ViewName.Extra:
				this.extraViewButton.interactable = false;
				break;
			default:
				return;
			}
			this.ShowHandles();
		}

		// Token: 0x06000CB4 RID: 3252 RVA: 0x00066DDC File Offset: 0x00064FDC
		private void OnHandleSelect(SkeletonEditHandle handle)
		{
			if (!handle.EditBone)
			{
				return;
			}
			EditBone siblingBone = handle.EditBone.SiblingBone;
			if (siblingBone && this.handleManager.HandleMap.ContainsKey(siblingBone.Key))
			{
				this.handleManager.HandleMap[siblingBone.Key].ShowPaired = true;
			}
		}

		// Token: 0x06000CB5 RID: 3253 RVA: 0x00066E48 File Offset: 0x00065048
		private void OnHandleDeselect(SkeletonEditHandle handle)
		{
			if (!handle.EditBone)
			{
				return;
			}
			EditBone siblingBone = handle.EditBone.SiblingBone;
			if (siblingBone && this.handleManager.HandleMap.ContainsKey(siblingBone.Key))
			{
				this.handleManager.HandleMap[siblingBone.Key].ShowPaired = false;
			}
		}

		// Token: 0x06000CB6 RID: 3254 RVA: 0x00066EB4 File Offset: 0x000650B4
		private void OnHandleSize(float size)
		{
			this.handleManager.SetHandleSize(size);
		}

		// Token: 0x06000CB7 RID: 3255 RVA: 0x00066EC2 File Offset: 0x000650C2
		private void OnPairBones()
		{
			this.handleManager.PairBones();
			this.SetConfigDirty(true);
		}

		// Token: 0x06000CB8 RID: 3256 RVA: 0x00066ED8 File Offset: 0x000650D8
		private void OnVisibility()
		{
			foreach (SkeletonEditHandle key in this.handleManager.TargetHandles)
			{
				string item = this.handleManager.KeyMap[key];
				List<string> keys;
				if (this.currentView == ViewName.Body)
				{
					keys = this.bodyView.Keys;
				}
				else if (this.currentView == ViewName.Hair)
				{
					keys = this.hairView.Keys;
				}
				else
				{
					if (this.currentView != ViewName.Extra)
					{
						continue;
					}
					keys = this.extraView.Keys;
				}
				if (keys.Contains(item))
				{
					keys.Remove(item);
				}
				else
				{
					keys.Add(item);
				}
			}
			this.ShowHandles();
			this.SetConfigDirty(true);
		}

		// Token: 0x06000CB9 RID: 3257 RVA: 0x00066FCC File Offset: 0x000651CC
		private void OnShowHidden()
		{
			this.showingHidden = !this.showingHidden;
			this.ShowHandles();
		}

		// Token: 0x06000CBA RID: 3258 RVA: 0x00066FE4 File Offset: 0x000651E4
		private void OnSave()
		{
			if (!this.handleManager.TargetEditor)
			{
				return;
			}
			this.handleManager.TargetEditor.SkeletonEdit.SaveConfig();
			this.bodyView.SaveKeys(this.handleManager.TargetEditor);
			this.hairView.SaveKeys(this.handleManager.TargetEditor);
			this.extraView.SaveKeys(this.handleManager.TargetEditor);
			this.SetConfigDirty(false);
		}

		// Token: 0x06000CBB RID: 3259 RVA: 0x00067068 File Offset: 0x00065268
		private void OnDiscard()
		{
			if (!this.handleManager.TargetEditor)
			{
				return;
			}
			this.handleManager.TargetEditor.SkeletonEdit.LoadConfigDataFromFile();
			this.bodyView.UpdateKeys();
			this.hairView.UpdateKeys();
			this.extraView.UpdateKeys();
			this.ShowHandles();
			this.SetConfigDirty(false);
		}

		// Token: 0x06000CBC RID: 3260 RVA: 0x000670D0 File Offset: 0x000652D0
		private void HideKeys(ICollection<string> keys)
		{
			foreach (string item in keys)
			{
				if (!this.hiddenKeys.Contains(item))
				{
					this.hiddenKeys.Add(item);
				}
			}
		}

		// Token: 0x06000CBD RID: 3261 RVA: 0x0006713C File Offset: 0x0006533C
		private void UnhideKeys(ICollection<string> keys)
		{
			foreach (string item in keys)
			{
				if (this.hiddenKeys.Contains(item))
				{
					this.hiddenKeys.Remove(item);
				}
			}
		}

		// Token: 0x06000CBE RID: 3262 RVA: 0x000671A8 File Offset: 0x000653A8
		private void HideHeadBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Transform boneTransform = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Head);
			HashSet<string> keys = this.FindChildBoneKeys(boneTransform, null);
			if (hideBones)
			{
				this.HideKeys(keys);
			}
			else
			{
				this.UnhideKeys(keys);
			}
			this.ShowHandles();
		}

		// Token: 0x06000CBF RID: 3263 RVA: 0x00067208 File Offset: 0x00065408
		private void HideArmBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Transform boneTransform = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.LeftUpperArm);
			Transform boneTransform2 = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.RightUpperArm);
			HashSet<string> keys = this.FindChildBoneKeys(boneTransform, null);
			HashSet<string> keys2 = this.FindChildBoneKeys(boneTransform2, null);
			if (hideBones)
			{
				this.HideKeys(keys);
				this.HideKeys(keys2);
			}
			else
			{
				this.UnhideKeys(keys);
				this.UnhideKeys(keys2);
			}
			this.ShowHandles();
		}

		// Token: 0x06000CC0 RID: 3264 RVA: 0x00067290 File Offset: 0x00065490
		private void HideLegBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Transform boneTransform = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.LeftUpperLeg);
			Transform boneTransform2 = component.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.RightUpperLeg);
			HashSet<string> keys = this.FindChildBoneKeys(boneTransform, null);
			HashSet<string> keys2 = this.FindChildBoneKeys(boneTransform2, null);
			if (hideBones)
			{
				this.HideKeys(keys);
				this.HideKeys(keys2);
			}
			else
			{
				this.UnhideKeys(keys);
				this.UnhideKeys(keys2);
			}
			this.ShowHandles();
		}

		// Token: 0x06000CC1 RID: 3265 RVA: 0x00067318 File Offset: 0x00065518
		private void HideTorsoBones(bool hideBones)
		{
			Humanoid component = this.handleManager.TargetEditor.GetComponent<Humanoid>();
			if (!component)
			{
				return;
			}
			Animator component2 = component.GetComponent<Animator>();
			List<Transform> list = new List<Transform>();
			Transform boneTransform = component2.GetBoneTransform(HumanBodyBones.Head);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.LeftUpperArm);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.RightUpperArm);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			boneTransform = component2.GetBoneTransform(HumanBodyBones.RightLowerLeg);
			if (boneTransform)
			{
				list.Add(boneTransform);
			}
			HashSet<string> keys = this.FindChildBoneKeys(component.transform, list);
			if (hideBones)
			{
				this.HideKeys(keys);
			}
			else
			{
				this.UnhideKeys(keys);
			}
			this.ShowHandles();
		}

		// Token: 0x06000CC2 RID: 3266 RVA: 0x00067404 File Offset: 0x00065604
		private HashSet<string> FindChildBoneKeys(Transform inTransform, List<Transform> stopPoints = null)
		{
			if (!inTransform)
			{
				return null;
			}
			if (stopPoints == null)
			{
				stopPoints = new List<Transform>();
			}
			HashSet<string> hashSet = new HashSet<string>();
			EditBone[] componentsInChildren = inTransform.GetComponentsInChildren<EditBone>();
			List<EditBone> list = new List<EditBone>();
			foreach (EditBone editBone in componentsInChildren)
			{
				if (stopPoints.Contains(editBone.RealTransform))
				{
					list.Add(editBone);
				}
				Dictionary<EditBone, string> keyMap = this.handleManager.TargetEditor.SkeletonEdit.KeyMap;
				if (keyMap.ContainsKey(editBone))
				{
					hashSet.Add(editBone.Key);
				}
			}
			EditBone componentInParent = inTransform.GetComponentInParent<EditBone>();
			if (componentInParent && componentInParent.RealTransform == inTransform)
			{
				hashSet.Add(componentInParent.Key);
			}
			foreach (EditBone editBone2 in list)
			{
				List<EditBone> list2 = new List<EditBone>();
				editBone2.GetComponentsInChildren<EditBone>(list2);
				foreach (EditBone editBone3 in list2)
				{
					hashSet.Remove(editBone3.Key);
				}
			}
			return hashSet;
		}

		// Token: 0x040011BD RID: 4541
		[SerializeField]
		private HandleManager handleManager;

		// Token: 0x040011BE RID: 4542
		[SerializeField]
		[Header("View References")]
		private BodyView bodyView;

		// Token: 0x040011BF RID: 4543
		[SerializeField]
		private HairView hairView;

		// Token: 0x040011C0 RID: 4544
		[SerializeField]
		private ExtraView extraView;

		// Token: 0x040011C1 RID: 4545
		[Header("Model Config")]
		[SerializeField]
		private Button pairBoneButton;

		// Token: 0x040011C2 RID: 4546
		[Header("View Config")]
		[SerializeField]
		private Button bodyViewButton;

		// Token: 0x040011C3 RID: 4547
		[SerializeField]
		private Button hairViewButton;

		// Token: 0x040011C4 RID: 4548
		[SerializeField]
		private Button extraViewButton;

		// Token: 0x040011C5 RID: 4549
		[SerializeField]
		private Button visibilityButton;

		// Token: 0x040011C6 RID: 4550
		[SerializeField]
		[Header("Visibility")]
		private Button showHiddenButton;

		// Token: 0x040011C7 RID: 4551
		[SerializeField]
		private Slider handleSizeSlider;

		// Token: 0x040011C8 RID: 4552
		[Header("Hide Toggles")]
		[SerializeField]
		private Toggle hideHeadToggle;

		// Token: 0x040011C9 RID: 4553
		[SerializeField]
		private Toggle hideTorsoToggle;

		// Token: 0x040011CA RID: 4554
		[SerializeField]
		private Toggle hideArmsToggle;

		// Token: 0x040011CB RID: 4555
		[SerializeField]
		private Toggle hideLegsToggle;

		// Token: 0x040011CC RID: 4556
		[SerializeField]
		[Header("Save Settings")]
		private Button saveButton;

		// Token: 0x040011CD RID: 4557
		[SerializeField]
		private Button discardButton;

		// Token: 0x040011CE RID: 4558
		private ViewName currentView;

		// Token: 0x040011CF RID: 4559
		private CharacterEditor previousTarget;

		// Token: 0x040011D0 RID: 4560
		private bool configIsDirty;

		// Token: 0x040011D1 RID: 4561
		private HashSet<string> hiddenKeys = new HashSet<string>();

		// Token: 0x040011D2 RID: 4562
		private bool showingHidden;
	}
}
